package org.ruiyichen.guizhou.agr.ui.mineview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import org.ruiyichen.guizhou.agr.R;

import java.util.List;

public class PieChartView extends View {
    private int arcColos[] = new int[]{getResources().getColor(R.color.green_light), Color.BLUE,
            getResources().getColor(R.color.red_light), Color.RED,
            getResources().getColor(R.color.light_red), Color.GREEN,
            getResources().getColor(R.color.title_bar), getResources().getColor(R.color.orange_light)};
    private final static String TAG = "PieChartView";

    private List<PieMember> data = null;

    //圆心位置
    private int centerX, centerY = 0;
    //整个饼图的半径
    private float radius = 0;
    //中间原型半径
    private float hollowRadius = 0;
    //饼图边距
    private float margin = 0;


    //饼图名称
    private String pieName = "得分比例";
    //lable高度
    private int lableHeight = 80;

    private float fontSize = 0;

    private Paint bgPaint = null;
    private Paint arcPaint = null;
    private Paint textPaint = null;

    public PieChartView(Context context) {
        super(context);
        this.init();
    }

    public PieChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public PieChartView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.init();
    }


    public void setPieName(String pieName) {
        this.pieName = pieName;
        postInvalidate();
    }

    private void init() {
        this.bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        this.bgPaint.setColor(getResources().getColor(R.color.white));
        this.bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        this.arcPaint = new Paint();
        this.arcPaint.setAntiAlias(true);
        this.arcPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        this.textPaint = new Paint();
        this.textPaint.setColor(getResources().getColor(R.color.white));
        this.textPaint.setAntiAlias(true);//去除锯齿
        this.textPaint.setFilterBitmap(true);//对位图进行滤波处理
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.drawColor(getResources().getColor(R.color.pageBgColor));
        canvas.drawColor(getResources().getColor(android.R.color.transparent));//设置背景透明


        if (margin == 0) {
            margin = getWidth() * 0.05f;
        }
        if (radius == 0) {
            if ((getHeight() - lableHeight) < getWidth()) {
                radius = (getHeight() - lableHeight - (margin * 2)) / 2;
            } else {
                radius = (getWidth() - (margin * 2)) / 2;
            }
        } else if ((radius * 2) > (getWidth() - (margin * 2))) {
            radius = (getWidth() - (margin * 2)) / 2;
        } else if ((radius * 2 + lableHeight) > (getHeight() - (margin * 2))) {
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = (int) (radius * 2d + lableHeight + (margin * 2d));
            setLayoutParams(params);
        }
        if (hollowRadius == 0) {
            hollowRadius = (int) (radius * 0.5f);
        }
        if (this.centerX == 0) {
            this.centerX = getWidth() / 2;
        }
        if (this.centerY == 0) {
            this.centerY = (getHeight() - lableHeight) / 2;
        }
        this.fontSize = scalaFonts((int) (hollowRadius / this.pieName.length() * 0.6f) * 2);

        //画最下一层的圆圈
        this.bgPaint.setColor(getResources().getColor(R.color.white));
        this.bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(centerX, centerY, radius, bgPaint);

        //画百分比圆弧
        this.drawArc(canvas);

        this.bgPaint.setColor(getResources().getColor(R.color.white));
        this.bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawCircle(centerX, centerY, hollowRadius, bgPaint);

        this.bgPaint.setStyle(Paint.Style.STROKE);
        this.bgPaint.setColor(getResources().getColor(R.color.blue_light));
        canvas.drawCircle(centerX, centerY, hollowRadius * 0.9f, bgPaint);

        drawText(canvas);

        canvas.restore();
    }

    public void drawText(Canvas canvas) {
        int x = getWidth();
        int y = getHeight() - lableHeight;

        this.textPaint.setTextSize(this.fontSize);
        float tX = (x - getFontlength(this.textPaint, this.pieName)) / 2;
        float tY = (y - getFontHeight(this.textPaint)) / 2 + getFontLeading(this.textPaint);
        canvas.drawText(this.pieName, tX, tY, this.textPaint);
    }

    public void drawArc(Canvas canvas) {
        RectF rect = new RectF();
        rect.left = centerX - radius;
        rect.top = centerY - radius;
        rect.right = centerX + radius;
        rect.bottom = centerY + radius;


        RectF rectText = new RectF();
        rectText.left = centerX - (radius - (radius * 0.4f));
        rectText.top = centerY - (radius - (radius * 0.4f));
        rectText.right = centerX + (radius - (radius * 0.4f));
        rectText.bottom = centerY + (radius - (radius * 0.4f));

        if (this.data == null || this.data.isEmpty()) {
            Log.w(TAG, "没有可以绘制的数据");
            arcPaint.setColor(arcColos[0]);
            canvas.drawArc(rect, //弧线所使用的矩形区域大小
                    0,  //开始角度
                    60, //扫过的角度
                    true, //是否使用中心
                    arcPaint);
            return;
        }
        int total = 0;//总数
        for (PieMember member : data) {
            total += member.getNumber();
        }
        float angle = 0f;
        int i = 0;
        for (int pointer = 0; pointer < data.size(); pointer++) {
            PieMember member = data.get(pointer);
            if (member.getColor() == 0) {
                if (i >= arcColos.length) {
                    i = 0;
                }
                member.setColor(arcColos[i++]);
            }
            float d = 360f * ((float) member.getNumber() / (float) total);

            arcPaint.setColor(member.getColor());
            canvas.drawArc(rect, angle, d, true, arcPaint);  //根据进度画圆弧

            Path path = new Path();
            path.addArc(rectText, angle, d);
            Paint citePaint = new Paint();
            citePaint.setTextSize(fontSize * 0.6F);
            citePaint.setStrokeWidth(1);
            citePaint.setColor(getResources().getColor(R.color.black));
            canvas.drawTextOnPath(member.getText(), path, 50, 0, citePaint);

            //在底部写文字，此处我们不需要
            float x = getWidth() / data.size() * pointer + margin;
            float y = getHeight() - margin;
            float h = getFontHeight(this.textPaint) * 0.6f;
            canvas.drawText(member.getText() + ":" + member.getNumber(), x + h + (h * 0.3f), y, citePaint);
            RectF r = new RectF(x, y - h, x + h, y);
            canvas.drawRect(r, arcPaint);
            angle += d;
        }

    }

    /**
     * 根据屏幕系数比例获取文字大小
     *
     * @return
     */
    private static float scalaFonts(int size) {
        //暂未实现
        return size;
    }

    /**
     * @return 返回指定笔和指定字符串的长度
     */
    public static float getFontlength(Paint paint, String str) {
        return paint.measureText(str);
    }

    /**
     * @return 返回指定笔的文字高度
     */
    public static float getFontHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.descent - fm.ascent;
    }

    /**
     * @return 返回指定笔离文字顶部的基准距离
     */
    public static float getFontLeading(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.leading - fm.ascent;
    }

    public void setData(List<PieMember> data) {
        this.data = data;
        postInvalidate();
    }


    public static class PieMember {
        private String text = "";
        private int number = 0;
        private int color = 0;

        public void augment(int n) {
            this.number += n;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }
    }
}
